#include "ExampleACUASButterfly5.h"

PROGRAM_INIT
	VARIABLE(A)
	VARIABLE(B)
	VARIABLE(C)
	VARIABLE(D)
	STRING(A_string,10)
	STRING(B_string,10)
	STRING(C_string,10)
	ACTIVATE_LCD
	CLEAR_LCD
	A=0;
	B=0;

	BEEP_VOLUME(50)

PROGRAM_START
	LCD_TEXT("HELLO")
	BUTTON(CENTER);
	SOUND();
	MENU();
PROGRAM_END


void SOUND (void)
	{
	BEEP(HZ,DUR)
	return 0;		
	}
	

void MENU (void)
{	
	int FLAG=1;

	int COUNT=1;
	int * COUNT_ptr = &COUNT;

	while(FLAG==1)
	{
		FLAG = DO_OPTION(COUNT_ptr);
	}//while
	return 0;	
}//MENU

int MENU_SELECTION (int * ptrCounter)
	{
		
		int to_return=0;
		ON_JOYSTICK_DOWN
		{
			WAIT_FOR_RELEASE_JOYSTICK_DOWN
			{
				SOUND();
				(*ptrCounter)++;
				if(*ptrCounter==5)
					*ptrCounter=1;
				to_return = 1;
			}//WAIT_FOR_RELEASE_JOYSTICK_DOWN	
		}//ON_JOYSTICK_DOWN					
		ON_JOYSTICK_UP
		{
			WAIT_FOR_RELEASE_JOYSTICK_UP
			{
				SOUND();
				(*ptrCounter)--;
				if(*ptrCounter==0)
					*ptrCounter=4;
				to_return = 1;
			}//WAIT_FOR_RELEASE_JOYSTICK_UP			
		}//ON_JOYSTICK_UP
		ON_JOYSTICK_CENTER
		{
			WAIT_FOR_RELEASE_JOYSTICK_CENTER
			{
				SOUND();
				to_return = EXEC_OPTION(ptrCounter);

			}//WAIT_FOR_RELEASE_JOYSTICK_UP			
		}//ON_JOYSTICK_UP
		return to_return;
	}

int DO_OPTION (int *Option_ptr)
{
	int Flag=1;
	switch(*Option_ptr)
	{
		case 1:	//BRIGHTNESS
				LCD_TEXT("LIGHT")
				Flag=IN_NEXT(Option_ptr);
				break;
		case 2: //TEMPERTATURE
				LCD_TEXT("TEMP")
				Flag=IN_NEXT(Option_ptr);
				break;
		case 3: //READ
				LCD_TEXT("READ")
				Flag=IN_NEXT(Option_ptr);
				break;
		case 4: //Flag = Out; //RETURN
				LCD_TEXT("END")
				Flag=IN_NEXT(Option_ptr);
				break;
		default:break;
	}//switch
	return Flag;
}//DO_OPTION



int EXEC_OPTION (int *Option_ptr)
{
	int Flag=1;
	switch(*Option_ptr)
	{
		case 1:	//BRIGHTNESS
				light();
				break;
		case 2: //TEMPERTATURE
				temp();
				break;
		case 3: //READ
				read();
				break;
		case 4: //Flag = Out; //RETURN
				LCD_TEXT("BYE!")
				WAIT_SEC(1)
				Flag=2;
				break;
		default:break;
	}//switch
	return Flag;
}//EXEC_OPTION

void IN_NEXT (int* Option_ptr)
{	
	int out;
	while(1)
	{
		out = MENU_SELECTION(Option_ptr);
		if(out>0)
			break;


	}
	return out;
}//IN_NEXT

void BUTTON (int Button)
{
	switch(Button)
		{
			case 1: WAIT_FOR_JOYSTICK_UP
					WAIT_FOR_RELEASE_JOYSTICK_UP
					break;
			case 2: WAIT_FOR_JOYSTICK_DOWN
					WAIT_FOR_RELEASE_JOYSTICK_DOWN
					break;
			case 3: WAIT_FOR_JOYSTICK_LEFT
					WAIT_FOR_RELEASE_JOYSTICK_LEFT
					break;
			case 4: WAIT_FOR_JOYSTICK_RIGHT
					WAIT_FOR_RELEASE_JOYSTICK_RIGHT
					break;
			case 5: WAIT_FOR_JOYSTICK_CENTER
					WAIT_FOR_RELEASE_JOYSTICK_CENTER
					break;
			default:DO_NOTHING
		}
	return 0;
	
}//BUTTON


int IF_BUTTON (int Button)
{
	int True=0;
	switch(Button)
		{
			case 1: ON_JOYSTICK_UP
					{
						WAIT_FOR_RELEASE_JOYSTICK_UP
						True = 1;
					}						
					break;
			case 2: ON_JOYSTICK_DOWN
					{
						WAIT_FOR_RELEASE_JOYSTICK_DOWN
						True = 1;
					}
					break;
			case 3: ON_JOYSTICK_LEFT
					{
						WAIT_FOR_RELEASE_JOYSTICK_LEFT
						True = 1;
					}
					break;
			case 4: ON_JOYSTICK_RIGHT
					{
						WAIT_FOR_RELEASE_JOYSTICK_RIGHT
						True = 1;
					}
					break;
			case 5: ON_JOYSTICK_CENTER
					{
						WAIT_FOR_RELEASE_JOYSTICK_CENTER
						True = 1;
					}
					break;
			default:break;
		}//switch
	return True;
}//IF_BUTTON

void light(void)
{
	float brightness;
	while(1)
	{
	BRIGHTNESS(brightness)
	LCD_D_NUMBER(brightness,4,1);
	WAIT_SEC(0.1)
	if(IF_BUTTON(CENTER))
		break;
	}
}


void temp(void)
{
	
	float temperature;
	while(1)
	{
	TEMPERATURE(temperature)
	LCD_D_NUMBER(temperature,4,1);
	WAIT_SEC(0.1)
	if(IF_BUTTON(CENTER))
		break;
	}
}

void read(void)
{
	while(1)
	{
	LCD_TEXT("r")
	if(IF_BUTTON(CENTER))
		break;
	}
}
